package com.dsd.datav.controller;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.dsd.datav.constants.DataTurn;
import com.dsd.datav.constants.Result;
import com.dsd.datav.domain.Datavsmap;
import com.dsd.datav.domain.User;
import com.dsd.datav.dto.SelectData;
import com.dsd.datav.dto.SortData;
import com.dsd.datav.enums.DatavsmapType;
import com.dsd.datav.enums.IdicatorColor;
import com.dsd.datav.mapper.UserMapper;
import com.dsd.datav.service.DatavsmapService;
import com.dsd.datav.service.UserService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import sun.util.calendar.BaseCalendar;
import sun.util.calendar.LocalGregorianCalendar;

import javax.lang.model.element.VariableElement;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @Title: DatavsController
 * @ProjectName datavsd
 * @Description: TODO
 * @date 2023/12/22 00222023/12/22 0022
 */
@RestController
@RequestMapping
public class DatavsController {
    @Autowired
    private DatavsmapService datavsMapService;
    @Autowired
    private UserService userService;
    @PostMapping("exportExcel")
    @ResponseBody
    public Result exportExcel(Datavsmap datavsmap) {
        List<Datavsmap> datavsmaps;
        String village = datavsmap.getVillage();
        String isCreditCard = datavsmap.getIsCreditCard();
        String isHpay = datavsmap.getIsHpay();
        String orgName = datavsmap.getOrgName();
        String orgId= datavsmap.getOrgId();
        System.out.println("orgId"+orgId+"村:"+village+"\n"+"是否有信用卡:"+isCreditCard+"\n"+"是否有惠支付:"+isHpay+"\n"+"镇:"+orgName);
        if (orgId.equals("6359")){
            if (orgName.isEmpty()){
                datavsmap.setOrgId("");
            }else{
                datavsmap.setOrgId(datavsmap.getOrgName());
            }

        }
        datavsmaps = datavsMapService.findForm(datavsmap);
        try {
            ExportParams exportParams = new ExportParams();
            exportParams.setType(ExcelType.XSSF);
            LocalDate currentDate = LocalDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
            String formattedDate = currentDate.format(formatter);
            Integer fileNum =1;

            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, Datavsmap.class, datavsmaps);
       /*     */
            String fileName=formattedDate+fileNum.toString();
            File file = new File("e:\\"+fileName+".xls");
            while (file.exists()){
               fileNum++;
                fileName=formattedDate+fileNum.toString();
                file = new File("e:\\"+fileName+".xls");
            }
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                workbook.write(fileOutputStream);
                fileOutputStream.close();
                workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
            return Result.failure();
        }
        return Result.success();
    }
    @ResponseBody
    @GetMapping("findForm")
    public Result findForm(Datavsmap datavsmap){
        List<Datavsmap> datavsmaps =new ArrayList<>();
        String village = datavsmap.getVillage();
        String isCreditCard = datavsmap.getIsCreditCard();
        String isHpay = datavsmap.getIsHpay();
        String orgName = datavsmap.getOrgName();
        String orgId= datavsmap.getOrgId();
        System.out.println("orgId"+orgId+"村:"+village+"\n"+"是否有信用卡:"+isCreditCard+"\n"+"是否有惠支付:"+isHpay+"\n"+"镇:"+orgName);
        if (orgId.equals("6359")||orgId.equals("AVSR")){
            if (orgName.isEmpty()){
                datavsmap.setOrgId("");
            }else{
                datavsmap.setOrgId(datavsmap.getOrgName());
            }

        }
        datavsmaps = datavsMapService.findForm(datavsmap);
        return Result.success(datavsmaps);
    }
    @ResponseBody
    @GetMapping("selectVillage")
    public Result selectVillage(String orgId){
        return Result.success(datavsMapService.selectVillage(orgId));
    }
    @ResponseBody
    @GetMapping("selectOrgName")
    public Result selectOrgName(){
        List<SelectData> selectData = datavsMapService.selectOrgName();
        List<SelectData> selectDataList=new ArrayList<>();
        for (SelectData selectDatum : selectData) {
            selectDatum.setOrgName(selectDatum.getOrgName().substring(16,18)+"镇");
            selectDatum.setOrgId(selectDatum.getOrgId());
            selectDataList.add(selectDatum);
        }
        return Result.success(selectData);
    }
    @ResponseBody
    @GetMapping("/queryAll")
    public Result queryAll(String orgId){
        List<Datavsmap> datavsmaps;
        if (orgId.equals("AW6N")||orgId.equals("6359")||orgId.equals("6357")||orgId.equals("6358")||orgId.equals("AVSR")||orgId.equals("6358")){
            datavsmaps = datavsMapService.queryAll();
        }else{
            datavsmaps= datavsMapService.queryByOrgId(orgId);
        }
        return Result.success(datavsmaps);
    }
    @ResponseBody
    @GetMapping("/queryByOrgId")
    public Result queryByOrgId(String orgId){
        List<Datavsmap> datavsmaps =datavsMapService.queryByOrgId(orgId);
        return Result.success(datavsmaps);
    }

    @ResponseBody
    @GetMapping("queryCount")
    public Result queryCount(){
        List<SortData> sortData = datavsMapService.queryCount();
        return Result.success(sortData);
    }
    @ResponseBody
    @GetMapping("sortFindAllByOrgIdANDType")
    public Result sortFindAllByOrgIdANDType(String orgId){
       List<SortData> sortData = datavsMapService.queryByOrgIdCount(orgId);
       return Result.success(sortData);
    }
    /**
     * 根据type查询
     * @param type
     * @return
     */
    @GetMapping("/findAllByType")
    public Result findAllByType(String type,String orgId){
        System.out.println(type);
        return Result.success(datavsMapService.findAllByType(type,orgId));
    }

    /**
     * 根据id查询详情
     * @return
     */
    @GetMapping("/findAllByName")
    public Result findAllByName(String name){

        return Result.success( datavsMapService.findAllByName(name));
    }
    @GetMapping("/testUser")
    public Result testUser(){
        List<User> users = userService.list();
        return Result.success(users);
    }
    @GetMapping("/")
    public Object test(){
        return "index.html";
    }
    /**
     *根据村查询
     */
    @GetMapping("/findAllByVillage")
    public Result findAllByVillage(String village){
        if (village.equals("锦屏镇")){
            return Result.success(datavsMapService.queryByOrgId("6361"));
        }
        if (village.equals("中都镇")){
            return Result.success(datavsMapService.queryByOrgId("6368"));
        }
        if (village.equals("屏边彝族乡")){
            return Result.success(datavsMapService.queryByOrgId("6365"));
        }
        else{
            return Result.success(datavsMapService.findAllByVillage(village));
        }
    }
}
